package com.lhjmall.member.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhjmall.common.exception.CommonException;
import com.lhjmall.common.utils.PageUtils;
import com.lhjmall.common.utils.Query;

import com.lhjmall.member.dao.UserDao;
import com.lhjmall.member.entity.UserEntity;
import com.lhjmall.member.fegin.CouponFegin;
import com.lhjmall.member.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Autowired
	private CouponFegin couponFegin;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public void saveV2(UserEntity user){
		QueryWrapper<UserEntity> queryWrapper=new QueryWrapper<UserEntity>().eq("name", user.getName());
		UserEntity userEntityInDb=this.getOne(queryWrapper);
		if(userEntityInDb!=null){
			throw new CommonException(400,"用户已经存在");
		}
		//保存用户
		this.save(user);
		queryWrapper=new QueryWrapper<UserEntity>().eq("name", user.getName());
		userEntityInDb=this.getOne(queryWrapper);
		couponFegin.sendCoupon(userEntityInDb.getId());
	}

}